package com.spittr.config;

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;


/**
 * 开启redis
 * @author zoe
 * 2017年2月22日
 */
@Configuration
@EnableCaching
public class RedisCacheConfig {
	
	/**
	 * 连接redis
	 * @return
	 */
	@Bean  
    public RedisConnectionFactory redisConnectionFactory() {  
        JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory();  
//        redisConnectionFactory.setHostName("127.0.0.1");  
//        redisConnectionFactory.setPort(6379);  
        return redisConnectionFactory;  
    }  
	
	/**
     * 缓存管理器
     * @param redisTemplate
     * @return
     */
    @Bean  
    public CacheManager cacheManager(RedisTemplate<String, String> redisTemplate) {  
        return new RedisCacheManager(redisTemplate);  
    }  
    
    /**
     * 数据访问模板
     * @param cf
     * @return
     */
    @Bean  
    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory cf) {  
    	RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();  
        redisTemplate.setConnectionFactory(cf);  
        return redisTemplate;  
    }  
  
    

}
